<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class member_model extends CI_Model {
	//모델 생성자 호출
	function __construct(){
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	 * 회원 내용 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select_member($id){
		$this->db->select("*");
	  	$this->db->where('id', $id);
	  	$query = $this->db->get('ch_member');
	  	//echo $this->db->last_query();
	  	$row = $query->row_array();
	  	
	  	$data['dbjob'] = 'i';
	  	$data['admin_yn'] = null;
	  	$data['membership'] = null;
	  	$data['id'] = null;
	  	$data['name'] =  null;
	  	$data['user_id'] = null;
	  	$data['birth_date '] = null;
	  	$data['gender'] = 'M';
	  	$data['solar_yn'] = 'Y';
	  	$data['zipcode1'] = null;
	  	$data['zipcode2'] = null;
	  	$data['addr1'] = null;
	  	$data['addr2'] = null;
	  	$data['tel1'] = null;
	  	$data['tel2'] = null;
	  	$data['tel3'] = null;
	  	$data['hp1'] = null;
	  	$data['hp2'] = null;
	  	$data['hp3'] = null;
	  	$data['sms_yn'] = 'Y';
	  	$data['mail_id'] =  null;
	  	$data['mail_domain'] =  null;
	  	$data['mailling_yn'] = 'Y';
	  	$data['corp_name'] = null;
	  	$data['corp_position'] = null;
	  	$data['corp_tel1'] = null;
	  	$data['corp_tel2'] = null;
	  	$data['corp_tel3'] = null;
	  	$data['corp_fax1'] = null;
	  	$data['corp_fax2'] = null;
	  	$data['corp_fax3'] = null;
	  	$data['corp_zipcode1'] = null;
	  	$data['corp_zipcode2'] = null;
	  	$data['corp_addr1'] = null;
	  	$data['corp_addr2'] = null;
	  	$data['baptism_yn'] = null;
	  	$data['church_name'] = null;
	  	$data['church_duty'] = null;
	  	$data['introducer'] = null;
	  	$data['join_path'] = null;
	  	
	  	
	  	if($row){
	  		$data['dbjob'] = 'u';
	  		$data['admin_yn'] = $row['admin_yn'];
	  		$data['membership'] = $row['membership'];
	  		$data['id'] = $id; 
	  		$data['name'] = $row['name'];
			$data['user_id'] =  $row['user_id'];
			$data['birth_date'] =  substr($row['birth_date'],0,10);
			$data['gender'] = $row['gender']; 
			$data['solar_yn'] = $row['solar_yn']; 
			$data['zipcode1'] = substr($row['post'],0,3); 
			$data['zipcode2'] = substr($row['post'],-3);
			$data['addr1'] = $row['addr1']; 
			$data['addr2'] = $row['addr2'];

			if($row['tel']){
				$tel = explode('-',$row['tel']);
				$data['tel1'] = $tel[0];
				$data['tel2'] = $tel[1];
				$data['tel3'] = $tel[2];
			}
			
	  		if($row['hp']){
				$hp = explode('-',$row['hp']);
				$data['hp1'] = $hp[0];
				$data['hp2'] = $hp[1];
				$data['hp3'] = $hp[2];
			}
			 
			$data['sms_yn'] = $row['sms_yn']; 
			
			if($row['email']){
				$arrMail = explode('@',$row['email']);
				$data['mail_id'] = $arrMail[0];
				$data['mail_domain'] = $arrMail[1];
			}else{
				$data['mail_id'] = null;
				$data['mail_domain'] = null;
			}
			
			$data['mailling_yn'] = $row['mailling_yn']; 
			$data['corp_name'] = $row['corp_name']; 
			$data['corp_position'] = $row['corp_position']; 
		  	
		  	if($row['corp_tel']){
				$corp_tel = explode('-',$row['corp_tel']);
				$data['corp_tel1'] = $corp_tel[0];
				$data['corp_tel2'] = $corp_tel[1];
				$data['corp_tel3'] = $corp_tel[2];
			}
			
		  	if($row['corp_fax']){
				$corp_fax = explode('-',$row['corp_fax']);
				$data['corp_fax1'] = $corp_fax[0];
				$data['corp_fax2'] = $corp_fax[1];
				$data['corp_fax3'] = $corp_fax[2];
			}
			
			$data['corp_zipcode1'] = substr($row['corp_post'],0,3); 
			$data['corp_zipcode2'] = substr($row['corp_post'],-3);
			$data['corp_addr1'] = $row['corp_addr1']; 
			$data['corp_addr2'] = $row['corp_addr2']; 
			$data['baptism_yn'] = $row['baptism_yn']; 
			$data['church_name'] = $row['church_name']; 
			$data['church_duty'] = $row['church_duty']; 
			$data['introducer'] = $row['introducer']; 
			$data['join_path'] = $row['join_path']; 
	  	}
	  	return $data;
	}
	
	/**
	 * 회원 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function list_where($param){
		$_where = null;
		$where = null;
		//키워드
		if($param['sch_keyname'] && $param['sch_keyword']){
			$array_keyname = explode(',',$param['sch_keyname']);
			foreach($array_keyname as $key){
				$_where[] = "{$key} LIKE '%{$param['sch_keyword']}%'";
			}
		}
		//성별
		if($param['sch_gender']) $_where[] = " gender in('{$param['sch_gender']}')";
		//생년월일
		if($param['sch_birth_start'] || $param['sch_birth_end']){
			if($param['sch_birth_start'] && $param['sch_birth_end']){
				$_where[] = " (birth_date BETWEEN '{$param['sch_birth_start']}' AND '{$param['sch_birth_end']}') ";
			}else if($param['sch_birth_start']){
				$_where[] = " birth_date >= '{$param['sch_birth_start']}' ";
			}else if($param['sch_birth_end']){
				$_where[] = " birth_date <= '{$param['sch_birth_end']}' ";
			}		
		}
		//등록일
		if($param['sch_reg_start'] || $param['sch_reg_end']){
			if($param['sch_reg_start'] && $param['sch_reg_end']){
				$_where[] = " (reg_date BETWEEN '{$param['sch_reg_start']}' AND '{$param['sch_reg_end']}') ";
			}else if($param['sch_reg_start']){
				$_where[] = " reg_date >= '{$param['sch_reg_start']}' ";
			}else if($param['sch_reg_end']){
				$_where[] = " reg_date <= '{$param['sch_reg_end']}' ";
			}		
		}
		
		if(is_array($_where)){
			$where = implode(' OR ', $_where);
		}
				
		return ($where) ? $this->db->where(trim($where)) : null; 
	}
	
	/**
	 * 회원 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_total($param){
		$this->list_where($param);
		$count = $this->db->count_all_results('ch_member');
		//echo $this->db->last_query();
		return $count;
	}
	
	/**
	 * 회원 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function list_member($param){
		$page = $param['page'];
		$listnum = $this->config->item('ch_listnum');
		$offset = $listnum*($page-1);
		$this->list_where($param);
		$this->db->order_by('id DESC');
		$query = $this->db->get('ch_member',$listnum,$offset);
		//echo $this->db->last_query();
		return $query->result();
	}
	
	
	/**
	 * 회원 관리자 메인 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function list_member_index($param){
		$this->db->where($param);
		$this->db->order_by('id DESC');
		$query = $this->db->get('ch_member',5,0);
		//echo $this->db->last_query();
		return $query->result();
	}
		
	/**
	 * 회원 아이디 유효성검사
	 *
	 * @access	public
	 * @return	integer
	 */
	function valid_userid_total($param){
		$this->db->where($param);
		$count = $this->db->count_all_results('ch_member');
		//echo $this->db->last_query();
		return $count;
	}
	
	/**
	 * 회원 db 처리
	 *
	 * @access	public
	 * @return	integer
	 */
	function member_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$result = $this->db->insert('ch_member', $data);
			break;
			case('u'):
				$this->db->where('id',$id);
				$result = $this->db->update('ch_member',$data);
			break;
			case('d'):
				$this->db->where('id', $id);
				$result = $this->db->update('ch_member',$data);
			break;
			
		}
	}
	
	/**
	 * 회원 약관 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select_member_terms(){
		$this->db->select("*");
		$query = $this->db->get('ch_terms_member');
		//echo $this->db->last_query();
		$row = $query->row_array();
		$data['dbjob'] = 'i';
		$data['id'] = null;
		$data['info_use'] = null;
		$data['info_personal'] = null;
		if($row){
			$data['dbjob'] = 'u';
			$data['id'] = $row['id'];
			$data['info_use'] = $row['info_use'];
			$data['info_personal'] = $row['info_personal'];
		}
		return $data;
	}
	
	/**
	 * 회원약관 db 처리
	 *
	 * @access	public
	 * @return	integer
	 */
	function member_term_dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$result = $this->db->insert('ch_terms_member', $data);
			break;
			case('u'):
				$this->db->where('id',$id);
				$result = $this->db->update('ch_terms_member',$data);
			break;
		
		}
	}
	
	/**
	 * 탈퇴회원 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function list_secede_where($param){
		$_where = null;
		$where = null;
		//등록일
		if($param['sch_reg_start'] || $param['sch_reg_end']){
			if($param['sch_reg_start'] && $param['sch_reg_end']){
				$_where[] = " (reg_date BETWEEN '{$param['sch_reg_start']}' AND '{$param['sch_reg_end']}') ";
			}else if($param['sch_reg_start']){
				$_where[] = " reg_date >= '{$param['sch_reg_start']}' ";
			}else if($param['sch_reg_end']){
				$_where[] = " reg_date <= '{$param['sch_reg_end']}' ";
			}
		}
		
		if(is_array($_where)){
			$where = implode(' OR ', $_where);
		}
		
		return ($where) ? $this->db->where(trim($where)) : null;
	}
	
	/**
	 * 탈퇴회원 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_secede_total($param){
		$this->list_secede_where($param);
		$count = $this->db->count_all_results('ch_secede_info');
		return $count;
	}
	
	/**
	 * 금일 탈퇴회원 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_secede_today(){
		$this->db->where('datediff(reg_date,now())',0);
		$count = $this->db->count_all_results('ch_secede_info');
		//echo $this->db->last_query();
		return $count;
	}
	
	/**
	 * 탈퇴회원 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function list_secede_member($param){
		$page = $param['page'];
		$listnum = $this->config->item('ch_listnum');
		$offset = $listnum*($page-1);
		$this->list_secede_where($param);
		$this->db->order_by('id DESC');
		$query = $this->db->get('ch_secede_info',$listnum,$offset);
		//echo $this->db->last_query();
		return $query->result();
	}
}
?>